<template>
<div class="com-container" @click="reverMap">
  <div class="com-chart" ref="map_ref"></div>
</div>
</template>

<script>
import axios from 'axios'
import { mapState } from 'vuex'
import { getProvinceMapInfo } from '../../utils/map_utils'
export default {
  data () {
    return {
      chartInstance: null,
      allData: null,
      mapData: {}
    }
  },
  created () {
    // ^在组件创建完成之后 进行回调函数的注册
    this.$socket.registerCallBack('mapData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    this.$socket.send({
      action: 'getData',
      socketType: 'mapData',
      chartName: 'map',
      value: ''
    })
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('mapData')
  },
  methods: {
    async initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme)
      // ^获取中国地图的矢量数据
      const ret = await axios.get('http://localhost:8080/static/map/china.json')
      // console.log(ret)
      this.$echarts.registerMap('china', ret.data)
      const initOption = {
        title: {
          text: '▋ 商家分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2e72bf',
            borderColor: '#333'
          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('click', async arg => {
        // console.log(arg)
        const provinceInfo = getProvinceMapInfo(arg.name)
        // console.log(provinceInfo)
        if (provinceInfo.key) {
          if (!this.mapData[provinceInfo.key]) {
            const ret = await axios.get('http://localhost:8080' + provinceInfo.path)
            this.mapData[provinceInfo.key] = ret.data
            // console.log(this.mapData[provinceInfo.key])
            this.$echarts.registerMap(provinceInfo.key, ret.data)
          }
        } else {
          return this.reverMap()
        }
        // ^判断省份点击过没有
        // ^获取点击省份地图矢量数据
        // console.log(provinceInfo.key)
        const changeOption = {
          geo: {
            map: provinceInfo.key
          }
        }
        this.chartInstance.setOption(changeOption)
      })
    },
    // ^获取数据
    getData (ret) {
      // ^await获取数据allData
      // const { data: res } = await this.$http.getMapData()
      this.allData = ret
      // console.log(this.allData)
      this.updateChart()
    },
    updateChart () {
      // ^处理数据
      const legenArr = this.allData.map(item => {
        return item.name
      })
      const seriesArr = this.allData.map(item => {
        return {
          type: 'effectScatter',
          itemStyle: {
            normal: {
              label: {
                show: true,
                formatter: item.name
              }
            }
          },
          rippleEffect: {
            scale: 5,
            brushType: 'stroke'
          },
          name: item.name,
          data: item.children,
          coordinateSystem: 'geo'
        }
      })
      const dataOption = {
        legend: { data: legenArr },
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter () {
      const titleFontSize = this.$refs.map_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: { fontSize: titleFontSize }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          title: {
            textStyle: { fontSize: titleFontSize / 2 }
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    // ^返回中国地图
    reverMap () {
      this.mapData = {}
      const reverOption = {
        geo: {
          map: 'china'
        }
      }
      this.chartInstance.setOption(reverOption)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updateChart() // 更新图表的展示
    }
  }
}
</script>

<style>

</style>
